package com.qf.android.sport.adapter.matchadapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qf.android.sport.R;
import com.qf.android.sport.entity.match.Contest;
import com.qf.android.sport.entity.match.PreContest;
import com.qf.android.sport.url.Cans;
import com.qf.android.sport.utils.match.DateUtil;
import com.squareup.picasso.Picasso;

import java.util.List;

/**
 * Created by Administrator on 2016/12/19.
 */

public class MatchRecyclerViewAdapter extends RecyclerView.Adapter<MatchRecyclerViewAdapter.MyMatchViewHolder> {
    public static final int FIRST_STICKY_VIEW = 1;//RecyclerView 的第一个item，肯定是展示StickyLayout的
    public static final int HAS_STICKY_VIEW = 2;//RecyclerView ，除了第一个item以外要展示StickyLayout的.
    public static final int NONE_STICKY_VIEW = 3;// RecyclerView 的不展示StickyLayout的item.
    private Context context;
    private List<PreContest> contest_list;

    public MatchRecyclerViewAdapter(Context context, List<PreContest> contest_list) {
        this.context = context;
        this.contest_list = contest_list;
    }


    @Override
    public MyMatchViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.match_recyclerview_item, parent, false);
        MyMatchViewHolder viewHolder = new MyMatchViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(MyMatchViewHolder holder, int position) {
        if (holder instanceof MyMatchViewHolder) {
            MyMatchViewHolder viewHolder = (MyMatchViewHolder) holder;
            Contest contest = contest_list.get(position).getContest();
            String time = contest.getStart_time();     //开始时间
            String newDate = DateUtil.getTraslateTime(time);//转换的北京时间

            String startTime = DateUtil.getStartTime(newDate);
            String date = DateUtil.getDate(newDate);
            String week = DateUtil.getWeek(newDate);
            String traslate = DateUtil.getTraslate(newDate);

            String homeTeamLogo = contest.getH_t().getLogo();//主队logo
            String awayTeamLogo = contest.getA_t().getLogo();//客队logo

            viewHolder.mDate.setText(date);
            viewHolder.mWeek.setText(week);
            if (traslate != null) {
                viewHolder.mTranslate.setText(traslate);
            } else {
                viewHolder.mTranslate.setVisibility(View.GONE);
            }
            viewHolder.mTime.setText(startTime);
            viewHolder.mCupName.setText(contest.getCup_name());//队伍总称
            viewHolder.mHomeTeamName.setText(contest.getH_t().getName());//主队名称
            viewHolder.mAwayTeamName.setText(contest.getA_t().getName());//客队名称

            //队伍logo下载
            Picasso.with(context).load(Cans.getLogo(homeTeamLogo)).into(viewHolder.mHomeTeamLogo);
            Picasso.with(context).load(Cans.getLogo(awayTeamLogo)).into(viewHolder.mAwayTeamLogo);

            //判断竞赛类型(0已经比赛，1未比赛)
            if (contest.getContest_type() == 0) {
                viewHolder.mEventRemind.setVisibility(View.INVISIBLE);
                viewHolder.mcoreSCard.setVisibility(View.VISIBLE);
                viewHolder.mHomeTeamScores.setText(contest.getHome_scores() + "");  //主队分数
                viewHolder.mAwayTeamScores.setText(contest.getAway_scores() + ""); //客队分数
            } else {
                viewHolder.mcoreSCard.setVisibility(View.INVISIBLE);
                viewHolder.mEventRemind.setVisibility(View.VISIBLE);
            }
            //关于吸顶布局

            if (position == 0) {
                viewHolder.mStickLayout.setVisibility(View.VISIBLE);
                viewHolder.itemView.setTag(FIRST_STICKY_VIEW);
            } else {
                String[] split = newDate.split(" ");
                String s = split[0];
                String traslateTime = DateUtil.getTraslateTime(contest_list.get(position - 1).getContest().getStart_time());
                String[] split1 = traslateTime.split(" ");
                String s1 = split1[0];
                if (!s.equals(s1)) {
                    viewHolder.mStickLayout.setVisibility(View.VISIBLE);
                    viewHolder.itemView.setTag(HAS_STICKY_VIEW);

                } else {
                    viewHolder.mStickLayout.setVisibility(View.GONE);
                    viewHolder.itemView.setTag(NONE_STICKY_VIEW);
                }
            }
        }
    }

    @Override
    public int getItemCount() {
        return contest_list.size();
    }

    public class MyMatchViewHolder extends RecyclerView.ViewHolder {
        private TextView mTime;            //比赛时间
        private TextView mCupName;         //队伍总称
        private ImageView mHomeTeamLogo;   //主队logo
        private TextView mHomeTeamName;    //主队名称
        private TextView mHomeTeamScores;  //主队分数
        private TextView mAwayTeamScores;  //客队分数
        private TextView mContestType;     //竞赛类型(0足球，1篮球)
        private LinearLayout mcoreSCard;   //计分板 (已结束)
        private ImageView mEventRemind;    //赛事提醒(未开始)
        private ImageView mAwayTeamLogo;   //客队logo
        private TextView mAwayTeamName;    //客队名称
        private TextView mDate;
        private TextView mWeek;
        private TextView mTranslate;
        private FrameLayout mStickLayout;

        public MyMatchViewHolder(View itemView) {
            super(itemView);
            mTime = (TextView) itemView.findViewById(R.id.tv_match_time);
            mCupName = (TextView) itemView.findViewById(R.id.tv_match_cup_name);
            mHomeTeamLogo = (ImageView) itemView.findViewById(R.id.iv_match_ht_logo);
            mHomeTeamName = (TextView) itemView.findViewById(R.id.tv_match_ht_name);

            mHomeTeamScores = (TextView) itemView.findViewById(R.id.tv_match_home_scores);
            mAwayTeamScores = (TextView) itemView.findViewById(R.id.tv_match_away_scores);
            mContestType = (TextView) itemView.findViewById(R.id.tv_match_contest_type);
            mcoreSCard = (LinearLayout) itemView.findViewById(R.id.LinearLayout_scorecard);
            mEventRemind = (ImageView) itemView.findViewById(R.id.iv_match_contest_type);

            mAwayTeamLogo = (ImageView) itemView.findViewById(R.id.iv_match_at_logo);
            mAwayTeamName = (TextView) itemView.findViewById(R.id.tv_match_at_name);
            mStickLayout = (FrameLayout) itemView.findViewById(R.id.sticky_header_view);
            mDate = (TextView) itemView.findViewById(R.id.tv_match_header_date);
            mWeek = (TextView) itemView.findViewById(R.id.tv_match_header_week);
            mTranslate = (TextView) itemView.findViewById(R.id.tv_match_header_translate);
        }
    }
}
